package com.yyi.springcloud.controller;


import com.yyi.springcloud.entities.SensorInfoModel;
import com.yyi.springcloud.entities.SensorInfoVo;
import com.yyi.springcloud.service.Local_AdditionFunctionService;
import com.yyi.springcloud.service.Local_SensorInfoService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.util.List;

@Controller
@RequestMapping("/mysqlapi/SensorInfoAPI")
public class Mysql_SensorInfoController {
	@Resource
	private Local_SensorInfoService sensorInfoService;
	@Resource
	private Local_AdditionFunctionService additionFunctionService;

	/**
	 * 1.根据条件查询传感器信息
	 * 
	 * @param primaryKey
	 * @return
	 */
	// http://localhost:8080/bxxs/api/SensorInfoAPI/getSensorInfoByCondition?labcode=Haierrefrigerator20151214&primaryKey=2017-11-2109:10:226
	@ResponseBody
	@RequestMapping(value = "/getSensorInfoByCondition", method = RequestMethod.GET)
	public List<SensorInfoModel> getSensorInfoByCondition(String labcode, String primaryKey) {
		try {
			primaryKey = new String(primaryKey.getBytes("iso-8859-1"), "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		SensorInfoModel sensorInfoModel = new SensorInfoModel();
		sensorInfoModel.setPrimarykey(primaryKey);
		sensorInfoModel.setSelected(1);// 只查询被选择的传感器
		//List<SensorInfoModel> list = sensorInfoService.getSensorInfoByCondition(sensorInfoModel);
		List<SensorInfoModel> list = additionFunctionService.getSensorContinus();
		for(SensorInfoModel sensorInfo:list){
			sensorInfo.setPrimarykey(primaryKey);
		}
		return list;
	}

	/**
	 * 2.查询出台位对应的所有未分组的传感器信息
	 * 
	 * @Description:
	 * @author 高杨
	 * @date 2018年8月24日 下午2:23:42
	 * @return List<SensorInfoModel>
	 */
	@ResponseBody
	@RequestMapping(value = "/listUngroupedSensors", method = RequestMethod.GET)
	public List<SensorInfoModel> listUngroupedSensors(String primaryKey) {
		try {
			primaryKey = new String(primaryKey.getBytes("iso-8859-1"), "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		SensorInfoModel sensorInfoModel = new SensorInfoModel();
		sensorInfoModel.setPrimarykey(primaryKey);
		// 只查询未分组的传感器
		sensorInfoModel.setSelected(0);
		return sensorInfoService.getSensorInfoByCondition(sensorInfoModel);
	}

	/**
	 * 3.根据primaryKey和传感器分组编号查询传感器信息
	 * 
	 * @param primaryKey
	 * @param groupNo
	 * @return
	 */
	// http://localhost:8080/bxxs/api/SensorInfoAPI/getSensorInfoByGroupNo?primaryKey=2017-11-2109:10:226&groupNo=1
	@ResponseBody
	@RequestMapping(value = "/getSensorInfoByGroupNo", method = { RequestMethod.GET, RequestMethod.POST })
	public List<SensorInfoModel> getSensorInfoByGroupNo(@RequestParam(required = true) String primaryKey,
			@RequestParam(required = true) String groupNo) {
		try {
			primaryKey = new String(primaryKey.getBytes("iso-8859-1"), "utf-8");
			groupNo = new String(groupNo.getBytes("iso-8859-1"), "utf-8");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sensorInfoService.getSensorInfoByGroupNo(primaryKey, groupNo);
	}

	/**
	 * 传感器设置后提交保存传感器信息
	 * 
	 * @Description:
	 * @author 高杨
	 * @date 2018年9月5日 下午4:00:32
	 * @return String
	 */
	@ResponseBody
	@RequestMapping(value = "/updateSensorsByPrimaryKey", method = { RequestMethod.POST })
	public String updateSensorsByPrimaryKey(@RequestBody SensorInfoVo sensorsVo) {
		for (SensorInfoModel model : sensorsVo.getSensors()) {
			sensorInfoService.updateSensorByPrimaryKeyAndSensorNo(sensorsVo.getPrimaryKey(), model);
		}
		return "success";
	}

}
